package com.joy.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joy.eduservice.entity.EduChapter;
import com.joy.eduservice.entity.EduVideo;
import com.joy.eduservice.entity.chapter.ChapterVo;
import com.joy.eduservice.entity.chapter.VideoVo;
import com.joy.eduservice.mapper.EduChapterMapper;
import com.joy.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joy.eduservice.service.EduVideoService;
import com.joy.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-02
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;


    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoCourseId(String courseId) {
        //1、根据课程id查询里面所有的章节
        QueryWrapper<EduChapter> eduChapterQueryWrapper = new QueryWrapper<>();
        eduChapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(eduChapterQueryWrapper);

        //2、根据课程id查询里面所有的小结
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id", courseId);
        List<EduVideo> eduVideos = eduVideoService.list(eduVideoQueryWrapper);

        //创建用于封装的最终数据的list集合
        ArrayList<ChapterVo> finalList = new ArrayList<>();
        //3、遍历查询章节list集合进行封装
        for(int i=0;i<eduChapters.size();i++){
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

            String id = eduChapter.getId();
            finalList.add(chapterVo);
            //4、遍历查询小节list集合，进行封装
            ArrayList<VideoVo> videoVos = new ArrayList<>();
            for (int j=0;j<eduVideos.size();j++){
                EduVideo eduVideo = eduVideos.get(j);
                if(id.equals(eduVideo.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
        }
        return finalList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查小节，如果没有小结，删除，如果有，不删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        //不用查询出数据，只需要查询出条数
        int count = eduVideoService.count(wrapper);
        if(count > 0){//查询出小节，不进行删除
            throw new GuliException(20001,"不能删除");
        }else{//不能查询出条数，进行删除
            int result = baseMapper.deleteById(chapterId);
            //成功返回true
            return result>0;
        }

    }

    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);

    }
}
